package mashuaiproject.mashuaiproject.serivce;

import mashuaiproject.mashuaiproject.mapper.FabulousMapper;
import mashuaiproject.mashuaiproject.model.clickfabulou;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FabulousSerivce {
    @Autowired
    private FabulousMapper fabulousMapper;


    public Boolean isClickFabulous(Integer fatherId) {
        //去数据库查找
        clickfabulou fabulous=fabulousMapper.selectOneFabulous(fatherId);
        if(fabulous==null){
            return true;//第一次操作这个文章
        }
        return false;
    }

    public void insertfabulous(clickfabulou fabulous) {
        fabulousMapper.insertfabulous1(fabulous);
    }

    public void updatefabulous(Integer fatherId,String type) {
        if(type.equals("up")){
            fabulousMapper.updatefabulous(fatherId,"true");
        }
        if(type.equals("up_no")){
            fabulousMapper.updatefabulous(fatherId,"false");
        }

        if(type.equals("down")){
            fabulousMapper.updatefabulouDown(fatherId,"true");
        }
        if(type.equals("down_no")){
            fabulousMapper.updatefabulouDown(fatherId,"false");
        }
    }

    public void updateUpCommentFabulous(Integer fatherId, String type, String type2) {
        if(type2.equals("up")){
            //查找up的当前点赞数
            Integer fabulous_up=fabulousMapper.selectFabulous_up(fatherId);
            if(type.equals("add")){
                fabulousMapper.updateUpCommentFabulous(fatherId);//点赞+1
            }else if(type.equals("reduces")){
                if(fabulous_up!=0){
                    fabulousMapper.updateUpCommentFabulousReduce(fatherId);//点赞-1；
                }
            }
        }

        if(type2.equals("down")){
            Integer fabulous_down=fabulousMapper.selectFabulous_down(fatherId);
            if(type.equals("add")){
                fabulousMapper.updateDownCommentFabulous(fatherId);
            }else if(type.equals("reduces")){
                if(fabulous_down!=0){
                    fabulousMapper.updateDownCommentFabulousReduce(fatherId);
                }
            }
        }
    }

    public int getabulous_up(Integer fatherId) {
        Integer fabulous_up=fabulousMapper.selectFabulous_up(fatherId);
        return  fabulous_up;
    }

    public Integer getabulous_down(Integer fatherId) {
        Integer fabulous_down=fabulousMapper.selectFabulous_down(fatherId);
        return fabulous_down;
    }
}
